package com.hua.api.modules.message.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.hua.api.core.constants.Status;
import com.hua.api.core.framework.sms.SmsSendThread;
import com.hua.api.core.framework.sms.SmsSenderProcess;
import com.hua.api.core.framework.util.DateUtil;
import com.hua.api.core.util.StringUtil;
import com.hua.api.modules.home.service.ActiveMqService;
import com.hua.api.modules.message.dao.SmsSenderDao;
import com.hua.api.modules.message.domain.SmsSender;
import com.hua.api.modules.message.repository.SmsSenderRepository;
import com.hua.api.modules.message.service.SmsSenderService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;


@Slf4j
@Service
@RequiredArgsConstructor
public class SmsSenderServiceImpl implements SmsSenderService {

    private final SmsSenderRepository smsSenderRepository;

    private final SmsSenderDao smsSenderDao;


    @Transactional(rollbackFor = Exception.class)
    @Override
    public void onSmsSender(JSONObject data) {
        log.info("onSmsSender data={}",data.toJSONString());
        JSONArray array = data.getJSONArray("phoneNumbers");
        String context = data.getString("context");
        for(int i=0;i<array.size();i++){
            String phoneNumber = array.getString(i);
            SmsSender smsSender = new SmsSender();
            smsSender.setPhoneNumber(phoneNumber);
            smsSender.setContext(context);
            smsSender.setState(Status.LOGIC_N);
            smsSender.setFailtime(0);
            smsSender.setCreatedTime(new Date());
            smsSenderRepository.save(smsSender);
        }
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void updateOnSmsSuccess(Integer id) {
        SmsSender sms = smsSenderRepository.findById(id).orElseGet(SmsSender::new);
        if(!StringUtil.isEmpty(sms.getId())){
            sms.setState(Status.LOGIC_Y);
            sms.setSendTime(new Date());
            smsSenderRepository.save(sms);
        }
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void updateOnSmsFail(Integer id) {
        SmsSender sms = smsSenderRepository.findById(id).orElseGet(SmsSender::new);
        if(!StringUtil.isEmpty(sms.getId())){
            sms.setFailtime(sms.getFailtime()+1);
            sms.setSendTime(new Date());
            smsSenderRepository.save(sms);
        }
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void smsSenderJob() {
        SmsSenderProcess process = new SmsSenderProcess();
        Date beginDate = DateUtil.offsetDate(new Date(),-1);
        List<SmsSender> list = smsSenderDao.queryNeedSendSms(beginDate);
        List<SmsSendThread> threads = list.stream().map(e-> new SmsSendThread(this, e)).collect(Collectors.toList());
        process.setList(threads);
        process.run();
    }

}